package com.fc.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class CustomWkbConverter {

    public static void main(String[] args) {
        double x = 116.4074;
        double y = 39.9042;

        String wkbHex = convertPointToWkbHex(x, y);
        System.out.println("Point(" + x + " " + y + ")");
        System.out.println("WKB  Hex: " + wkbHex);
        System.out.println("Expected: 0101000000FC1873D7121A5D4088855AD3BCF34340");
    }

    public static String convertPointToWkbHex(double x, double y) {
        // 创建字节缓冲区（小端序）
        ByteBuffer buffer = ByteBuffer.allocate(21)
                .order(ByteOrder.LITTLE_ENDIAN);

        // 字节顺序标记 (1 = 小端序)
        buffer.put((byte) 0x01);

        // 几何类型 (1 = Point)
        buffer.putInt(0x00000001);

        // X 坐标 (1.0)
        buffer.putDouble(x);

        // Y 坐标 (2.0)
        buffer.putDouble(y);

        // 转换为字节数组
        byte[] wkb = buffer.array();

        // 转换为十六进制字符串
        return bytesToHex(wkb);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = String.format("%02X", b & 0xFF);
            hexString.append(hex);
        }
        return hexString.toString();
    }
}